import { queryNotices } from '../services/api';
import { getCarList, getCarAllList, delCarGoods, delCarAll, editCar } from '../services/car';


export default {
  namespace: 'global',
  state: {
    collapsed: false,
    isEdit: true,
    pageAll: 0,
    tages: [],
    carListNum: 0,
    orderDetial: [],
    carAllList: [],
    carListAll: [],
  },

  effects: {
    *getCarAllData({ payload }, { call, put }) {
      const response = yield call(getCarAllList, payload);
      yield put({
        type: 'getCarListNum',
        payload: response.data,
      });
      yield put({
        type: 'internaltradetypeDetail/getAllCarsData',
        payload: response.data,
      });
    },
  },

  reducers: {
    getCarListNum(state, action) {
      let s = new Set();
      // console.log(action.payload);
      action.payload.map(item => s.add(item.internalType));
      // console.log(s);
      s = Array.from(s);
      let pageAll = parseInt(action.payload.length / 3, 10);
      if (action.payload.length % 3) {
        pageAll += 1;
      }
      const pageNumber = 3;
      let i = 0;
      const dataList = [];
      for (i = 1; i <= pageAll; i += 1) {
        let obj = [];
        obj = action.payload.slice(3 * (i - 1), (3 * (i - 1)) + pageNumber);
        dataList.push(obj);
      }
      return {
        ...state,
        carListNum: action.payload.length,
        orderDetial: action.payload,
        carAllList: action.payload,
        carListAll: dataList,
        tages: s,
        pageAll,
      };
    },
    getTags(state, action) {
      // console.log(state.carAllList);

      return {
        ...state,
        tages: s,
      };
    },
    orderDetials(state, { payload }) {
      let array = [];
      const result = state.carAllList;
      if (payload.length !== 0) {
        array = result.filter((element, index) => {
          const number = payload.indexOf(element.internalType);
          if (number !== -1) {
            return element;
          }
        });
      } else {
        array = state.carAllList;
      }
      return {
        ...state,
        orderDetial: array,
      };
    },
  },

  subscriptions: {
    setup({ dispatch }) {
      dispatch({
        type: 'getCarAllData',
        payload: {
          customerId: 1,
          pageNum: 0,
        },
      });
    },
  },
};

